$(function () {
    let url = '../return/list';
    $("#jqGrid").Grid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '订单号', name: 'orderSn', index: 'order_sn', width: 100},
            {label: '商品名称', name: 'goodsName', index: 'goods_name', width: 80},
            {label: '退货类型', name: 'returnTypeDesc', index: 'return_type', width: 60},
            {
                label: '申请时间', name: 'returnApplyTime', index: 'return_apply_time', width: 80,
                formatter: function (value) {
                    return transDate(value);
                }
            },
            {
                label: '审核状态', name: 'returnAuditStatusDesc', index: 'return_audit_status', width: 80
            },
            {
                label: '退货状态', name: 'returnStatusDesc', index: 'return_status', width: 80
            }
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        returnGoods: {},
        q: {
            orderSn: ''
        },
        ruleValidate: {
            returnAuditRemark: [
                {required: true, message: '审核结论不能为空', trigger: 'blur'}
            ],
            returnAuditStatus: [
                {required: true, pattern:'^(?!1).*$',  message: '请选择审核结果', trigger: 'change'}
            ]
        }
    },


    methods: {
        query: function () {
            vm.reload();
        },
        audit: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "审核";
            vm.getInfo(id);
        },
        saveAudit: function () {
            var url = "../return/saveAudit";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.returnGoods),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        close: function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }

            vm.returnGoods.id = id;

            confirm('确定要关闭？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../return/closeReturn",
                    contentType: "application/json",
                    params: JSON.stringify(vm.returnGoods),
                    successCallback: function (r) {
                        if (r.code != 0){
                            alert(r.msg);
                        } else {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }

                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../return/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.returnGoods = r.returnGoods;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.detail = false;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'orderSn': vm.q.orderSn
                },
                page: page
            }).trigger("reloadGrid");
        },
        eyeImageGoodsPicUrl: function () {
            var url = vm.returnGoods.listPicUrl;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic1Url: function () {
            var url = vm.returnGoods.returnPic1Url;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic2Url: function () {
            var url = vm.returnGoods.returnPic2Url;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic3Url: function () {
            var url = vm.returnGoods.returnPic3Url;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic4Url: function () {
            var url = vm.returnGoods.returnPic4Url;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic5Url: function () {
            var url = vm.returnGoods.returnPic5Url;
            eyeImage(url);
        },
        eyeImageGoodsReturnPic6Url: function () {
            var url = vm.returnGoods.returnPic6Url;
            eyeImage(url);
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveAudit()
            });
        }
    }
});